package com.geen.tuoke.base

import android.content.res.Resources
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.geen.tuoke.R
import com.geen.tuoke.utils.AppManager
import com.geen.tuoke.utils.ext.dismissLoadingExt
import com.geen.tuoke.utils.ext.showLoadingExt
import com.kbeanie.multipicker.api.entity.ChosenImage
import com.kbeanie.multipicker.api.ui.PickImageActivity
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import kotlinx.coroutines.launch
import me.jessyan.autosize.AutoSizeCompat
import me.jessyan.autosize.AutoSizeConfig
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.lang.reflect.ParameterizedType


abstract class BaseActivity<VM : BaseViewModel, T : ViewDataBinding> : PickImageActivity() {
    lateinit var mBinding: T

    lateinit var mViewModel: VM
    lateinit var mViewModelFactory: ViewModelProvider.Factory

    inline fun <reified VM> classOf() = VM::class.java

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        QMUIStatusBarHelper.translucent(this)
        mBinding = DataBindingUtil.setContentView(this, getLayoutId())
        mViewModel = ViewModelProvider(this).get(getVmClazz(this))
        registerCommonUiCall()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        AppManager.appManager?.addActivity(this)
        initData()
    }

    open fun initData() {}

    private fun registerCommonUiCall() {

        mViewModel.commonUILiveData.showDialog.observe(this, {
            showLoading(
                if (it.isEmpty()) {
                    getString(R.string.text_loading_message)
                } else it
            )
        })
        mViewModel.commonUILiveData.dismissDialog.observe(this, {
            dismissLoading()
        })
    }

    abstract fun getLayoutId(): Int

    @Suppress("UNCHECKED_CAST")
    fun <VM> getVmClazz(obj: Any): VM {
        return (obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as VM
    }


    override fun onDestroy() {
        super.onDestroy()
        mBinding.unbind()
        dismissLoading()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    fun showLoading(message: String = getString(R.string.text_loading_message)) {
        showLoadingExt(message)

    }

    open fun dismissLoading() {
        dismissLoadingExt()
    }

    override fun onImagesChosen(p0: MutableList<ChosenImage>?) {

    }

    override fun onError(p0: String?) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventCome(eventCode: String) {

    }

    //执行系统的 Resources前  执行兼容方案
    override fun getResources(): Resources {
        try {
            lifecycleScope.launch {
                val width = AutoSizeConfig.getInstance().screenWidth
                val height = AutoSizeConfig.getInstance().screenHeight
                AutoSizeCompat.autoConvertDensity(
                    super.getResources(), 375f,
                    width < height
                );
            }


        } catch (e: Exception) {
        }
        return super.getResources()
    }

    fun getSuperResources(): Resources {
        return super.getResources()
    }

}
